import { resolve } from 'path';
import { defineConfig } from 'vite';
import { createStyleImportPlugin, AntdResolve } from 'vite-plugin-style-import';

import vue from '@vitejs/plugin-vue';
import compression from 'vite-plugin-compression';

const outDir = './dist';
const targetUrl = 'http://localhost:8881/';

// https://vitejs.dev/config/
export default defineConfig({
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true
      }
    }
  },
  plugins: [
    vue(),
    createStyleImportPlugin({
      resolves: [AntdResolve],
      libs: [
        {
          libraryName: 'ant-design-vue',
          esModule: true,
          resolveStyle: name => `ant-design-vue/es/${name}/style/css`
        }
      ]
    }),
    compression()
  ],
  resolve: {
    alias: {
      '@': resolve(__dirname, './src')
    }
  },
  server: {
    open: true,
    host: '0.0.0.0',
    port: 7100,
    cors: true,
    proxy: {
      '^/api': { ws: true, changeOrigin: true, target: targetUrl },
      '^/.+/api': { ws: true, changeOrigin: true, target: targetUrl }
    }
  },
  build: {
    target: 'es6',
    outDir: outDir,
    minify: 'terser',
    emptyOutDir: true,
    terserOptions: {
      compress: {
        keep_infinity: true,
        // Used to delete console in production environment
        drop_console: true
      }
    },
    // Turning off brotliSize display can slightly reduce packaging time
    brotliSize: false,
    chunkSizeWarningLimit: 2000
  }
});
